package com.old.mapper;

import com.github.pagehelper.Page;
import com.old.dto.PostQueryDTO;
import com.old.entity.Post;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PostMapper {

    /**
     * 分页查询帖子列表（带搜索和排序）
     */
    @Select("SELECT * FROM post WHERE title LIKE CONCAT('%', #{queryDTO.searchTerms}, '%') ORDER BY create_time DESC")
    List<Post> getPostList(@Param("queryDTO") PostQueryDTO queryDTO);

    /**
     * 获取所有帖子列表
     */
    @Select("SELECT id, image_url, name, author_id, create_time, update_time, avatar, title, content FROM post ORDER BY create_time DESC")
    List<Post> getPostList1();

    /**
     * 发布帖子
     */
    //初始化likeman为0
    //插入式时设置为审核态
    @Insert("INSERT INTO post(image_url, name, author_id, create_time, update_time, avatar, title, content, likeMan, isShow) " +
            "VALUES(#{imageUrl}, #{name}, #{authorId}, #{createTime}, #{updateTime}, #{avatar}, #{title}, #{content}, #{likeMan}, 0)")
    @Options(useGeneratedKeys = true, keyProperty = "id") // 获取自增主键
    void createPost(Post post);

    /**
     * 获取帖子详情
     */
    @Select("SELECT id, image_url, name, author_id, create_time, update_time, avatar, title, likeMan,content FROM post WHERE id = #{id} and isShow = 1")
    Post getPostDetail(Integer id);

    //根据accountId查帖子
    @Select("SELECT id, image_url, name, author_id, create_time, update_time, avatar, title, content FROM post WHERE author_id = #{accountId}")
    List<Post> findByAccountId(Integer accountId);

    //获取帖子列表-分页-支持条件查询
    Page<Post> postListPage(PostQueryDTO postQueryDTO);

    // 删除帖子
    @Delete("DELETE FROM post WHERE id = #{postId}")
    void deletePost(Integer postId);

    Page<Post> list(Integer sort, String searchTerms);

    String getLikeManByPostId(Integer postId);

    void updateLikeMan(Integer postId, String likeMan);

    //审核帖子
    @Update("UPDATE post SET isShow = 1 WHERE id = #{postId}")
    void updatePostStatus(Integer postId);

    //检查帖子是否存在
    @Select("SELECT * FROM post WHERE id = #{postId}")
    Post checkPostExist(Integer postId);

    void batchDeleteByIds(List<Integer> ids);

    // 新增方法
    List<Post> selectPostsByIds(@Param("ids") List<Integer> ids);

    //按顺序获取推荐列表
    List<Post> selectPostsWithRecommendFlag(List<Integer> ids);

    // 随机顺序获取推荐列表
    List<Post> selectPostsWithRandom(List<Integer> ids);

}
